package com.caseprocessing.model;

/**
 * 边界框模型
 * 表示页面中元素的位置和尺寸
 */
public class BoundingBox {
    
    private int x;      // 左上角X坐标
    private int y;      // 左上角Y坐标
    private int width;  // 宽度
    private int height; // 高度
    
    public BoundingBox() {}
    
    public BoundingBox(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    
    // Getters and Setters
    public int getX() { return x; }
    public void setX(int x) { this.x = x; }
    
    public int getY() { return y; }
    public void setY(int y) { this.y = y; }
    
    public int getWidth() { return width; }
    public void setWidth(int width) { this.width = width; }
    
    public int getHeight() { return height; }
    public void setHeight(int height) { this.height = height; }
    
    /**
     * 计算边界框的面积
     */
    public int getArea() {
        return width * height;
    }
    
    /**
     * 获取右下角坐标
     */
    public int getRightX() {
        return x + width;
    }
    
    public int getBottomY() {
        return y + height;
    }
    
    /**
     * 检查两个边界框是否重叠
     */
    public boolean overlaps(BoundingBox other) {
        return !(this.getRightX() < other.getX() || 
                 other.getRightX() < this.getX() ||
                 this.getBottomY() < other.getY() || 
                 other.getBottomY() < this.getY());
    }
    
    /**
     * 计算与另一个边界框的重叠面积
     */
    public int getOverlapArea(BoundingBox other) {
        if (!overlaps(other)) {
            return 0;
        }
        
        int overlapX = Math.max(0, Math.min(this.getRightX(), other.getRightX()) - 
                                   Math.max(this.getX(), other.getX()));
        int overlapY = Math.max(0, Math.min(this.getBottomY(), other.getBottomY()) - 
                                   Math.max(this.getY(), other.getY()));
        
        return overlapX * overlapY;
    }
    
    /**
     * 检查当前边界框是否包含指定点
     */
    public boolean contains(int pointX, int pointY) {
        return pointX >= x && pointX <= getRightX() && 
               pointY >= y && pointY <= getBottomY();
    }
    
    @Override
    public String toString() {
        return String.format("BoundingBox{x=%d, y=%d, width=%d, height=%d}", x, y, width, height);
    }
}